/*
    13. Write a program in C to insert a new node at the beginning in a doubly linked list. 
    Test Data and Expected Output :

    Input the number of nodes : 3                                                                                
    Input data for node 1 : 2                                                                                    
    Input data for node 2 : 5                                                                                    
    Input data for node 3 : 8                                                                                    
                                                                                                                
    Data entered in the list are :                                                                               
    node 1 : 2                                                                                                   
    node 2 : 5                                                                                                   
    node 3 : 8                                                                                                   
    Input data for the first node : 1                                                                            
                                                                                                                
    After insertion the new list are :                                                                           
    node 1 : 1                                                                                                   
    node 2 : 2                                                                                                   
    node 3 : 5                                                                                                   
    node 4 : 8  
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node* List;
typedef struct node{
    int x;
    List pre;
    List next;
} Node;

List createDoubleList(int n)
{
    List root = NULL;
    List newNode = NULL;
    List curNode = NULL;
    int i;
    for(i = 0; i < n; i++){
        newNode = (List)malloc(sizeof(Node));
        if(!newNode){
            printf("Memery is not enough!\n");
            return NULL;
        }
        printf("Input data for node %d : ", i);
        scanf("%d", &(newNode->x));
        newNode->next = NULL;
        if(!root){
            newNode->pre = newNode;
            root = newNode;
            curNode = root;
        }else{
            newNode->pre = curNode;
            curNode->next = newNode;
            curNode = newNode;
        }
    }
    return root;
}

void printDoubleList(List root)
{
    List list = root;
    if(list == NULL){
        printf("The list is empty!\n");
        return;
    }

    while(list != NULL){
        printf("Data = %d\n", list->x);
        list = list->next;
    }
}

void printDoubleListAddr(List root)
{
    List list = root;
    if(list == NULL){
        printf("The list is empty!\n");
        return;
    }    

    while(list != NULL){
        printf("|%p|%p|%d|%p| <=> ", list, list->pre, list->x, list->next);
        list = list->next;
    } 
    printf("\n");       
}

List insertNodeAtBegin(List root)
{
    List curNode = root;
    List newNode = (List)malloc(sizeof(Node));
    if(!newNode){
        printf("Memery is not enough!\n");
        return NULL;
    }
    printf("Input data for first node : ");
    scanf("%d", &(newNode->x));
    newNode->next = curNode;
    newNode->pre = newNode;
    curNode->pre = newNode;
    root = newNode;
    
    return root;
}

int main(void)
{
    List root = NULL;
    int n;
    printf("Input the number of nodes : ");
    scanf("%d", &n);
    root = createDoubleList(n);

    printf("\nData entered on the list are :\n");
    printDoubleList(root);
    printDoubleListAddr(root);

    root = insertNodeAtBegin(root);
    printf("\nAfter insertion the new list are : \n");
    printDoubleList(root);
    printDoubleListAddr(root);
 
    return 0;
}